package com.yc.cloud.portal.config;


import com.yc.cloud.portal.service.MemberAccountService;
import com.yc.cloud.security.component.DynamicSecurityService;
import com.yc.cloud.security.config.BaseSecurityConfig;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * mall-security模块相关配置
 * Created by macro on 2019/11/9.
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends BaseSecurityConfig {

    @Resource
    private MemberAccountService memberAccountService;




//    @Resource
//    private UmsResourceService resourceService;

    @Bean
    public UserDetailsService userDetailsService() {
        //获取登录用户信息
        return username -> memberAccountService.loadUserByUsername(username);
    }

    @Bean
    public DynamicSecurityService dynamicSecurityService() {
        return new DynamicSecurityService() {
            @Override
            public Map<String, ConfigAttribute> loadDataSource() {

//                List<UmsResource> resourceList = resourceService.list();
//                if(CollUtil.isNotEmpty(resourceList)){
//                    Map<String, ConfigAttribute> map = new ConcurrentHashMap<>(resourceList.size());
//                    for (UmsResource resource : resourceList) {
//                        map.put(resource.getUrl(), new org.springframework.security.access.SecurityConfig(resource.getId() + ":" + resource.getName()));
//                    }
//                    return map;
//                }
                return new ConcurrentHashMap<>(0);
            }
        };
    }
}
